package com.example.sellqq.service.impl;

import com.example.sellqq.domain.Record;
import com.example.sellqq.repository.RecordRepository;
import com.example.sellqq.service.RecordService;
import com.example.sellqq.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: thekernel
 * @Date: 2020/4/20 11:55
 * @Version: 1.0
 * @Description:
 */
@Service(value = "RecordService")
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordRepository recordRepository;

    /**
     * 保存多个通话记录
     *
     * @param records 通话记录
     * @return ResponseVO.SUCCESS: 保存成功 ResponseVO.FAIL: 保存失败
     */
    @Override
    public ResponseVO saveRecords(List<Record> records) {
        List<Record> recordList = this.recordRepository.saveAll(records);
        return ResponseVO.success("保存成功", recordList);
    }

    /**
     * 保存单个通话记录
     *
     * @param record 通话记录
     * @return ResponseVO.SUCCESS: 保存成功 ResponseVO.FAIL: 保存失败
     */
    @Override
    public ResponseVO saveRecord(Record record) {
        record.setStatus(Boolean.TRUE);
        record.setCreateTime(new Date());
        Record dbRecord = this.recordRepository.save(record);
        return ResponseVO.success("保存成功", dbRecord);
    }

    /**
     * 读取所有通话记录
     *
     * @return 通话记录
     */
    @Override
    public ResponseVO readAllRecords() {
        return ResponseVO.success("读取成功", this.recordRepository.findAll(Sort.by(Sort.Direction.DESC, "createTime")));
    }
}
